package com.invengo.module_check.ui.fragment.book_opeartion_result;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.invengo.library_db.entity.module_check.BookBean;
import com.invengo.library_db.entity.module_check.ShelfBean;
import com.invengo.module_check.BR;
import com.invengo.module_check.R;
import com.invengo.module_check.data.entity.ActionArgs;
import com.invengo.module_check.databinding.FragmentBookOpeartionResultBinding;

import java.util.List;

import androidx.annotation.Nullable;
import androidx.navigation.fragment.NavHostFragment;
import me.goldze.mvvmhabit.base.BaseFragment;

import static com.invengo.module_check.data.entity.BundleKey.KEY_ACTION;
import static com.invengo.module_check.data.entity.BundleKey.KEY_BOOKS;
import static com.invengo.module_check.data.entity.BundleKey.KEY_OP_STATE;
import static com.invengo.module_check.data.entity.BundleKey.KEY_SHELFS;

public class BookOpeartionResultFragment extends BaseFragment<FragmentBookOpeartionResultBinding, BookOpeartionResultViewModel> {
    List<BookBean> bookBeans;
    List<ShelfBean> shelfBeans;
    ActionArgs actionArgs;

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_book_opeartion_result;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initData() {
        bookBeans = (List<BookBean>) getArguments().getSerializable(KEY_BOOKS);
        shelfBeans = (List<ShelfBean>) getArguments().getSerializable(KEY_SHELFS);
        actionArgs = (ActionArgs) getArguments().getSerializable(KEY_ACTION);
        switch (actionArgs){
            case ACTION_FIRST:
                binding.bookRecycler.setVisibility(View.GONE);
                viewModel.addBookList(actionArgs, null,shelfBeans, getArguments().getBoolean(KEY_OP_STATE));
                break;
            default:
                binding.shelfRecycler.setVisibility(View.GONE);
                viewModel.addBookList(actionArgs, bookBeans,null, getArguments().getBoolean(KEY_OP_STATE));
                break;
        }
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.ui.lanchMainEvent.observe(this, unused -> NavHostFragment.findNavController(BookOpeartionResultFragment.this).popBackStack(actionArgs.getHomeAction(), false));
        viewModel.ui.back2workEvent.observe(this, unused -> NavHostFragment.findNavController(BookOpeartionResultFragment.this).popBackStack(actionArgs.getWorkAction(), false));
    }
}
